package com.like.miaosha.controller;

import com.like.miaosha.domain.MiaoshaUser;
import com.like.miaosha.redis.GoodsKey;
import com.like.miaosha.redis.RedisService;
import com.like.miaosha.result.Result;
import com.like.miaosha.service.GoodsService;
import com.like.miaosha.service.MiaoshaUserService;
import com.like.miaosha.vo.GoodsDetailVo;
import com.like.miaosha.vo.GoodsVo;
import javafx.application.Application;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.spring4.context.SpringWebContext;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    MiaoshaUserService miaoshaUserService;

    @Autowired
    RedisService redisService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    ThymeleafViewResolver thymeleafViewResolver;

    @Autowired
    ApplicationContext applicationContext;

    @RequestMapping(value="/to_list",produces = "text/html")
    @ResponseBody
    public String toList(HttpServletRequest req, HttpServletResponse resp, Model model, MiaoshaUser user){
        String html=redisService.get(GoodsKey.getGoodsList,"",String.class);
        if(!StringUtils.isEmpty(html))
            return html;
        List<GoodsVo> goodsList = goodsService.listGoodsVo();
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("user",user);
        SpringWebContext ctx=new SpringWebContext(req,resp,req.getServletContext(),req.getLocale(),model.asMap(),applicationContext);

        //手动渲染
        html =thymeleafViewResolver.getTemplateEngine().process("goods_list",ctx);
        if(!StringUtils.isEmpty(html)){
            redisService.set(GoodsKey.getGoodsList,"",html);
        }
        return html;
    }


    @RequestMapping(value="/to_detail2/{goodsId}",produces = "text/html")
    @ResponseBody
    public String detail2(HttpServletRequest req, HttpServletResponse resp,Model model, MiaoshaUser user,
                         @PathVariable("goodsId")long goodsId){
        GoodsVo goods =goodsService.getGoodsVoByGoodsId(goodsId);
        model.addAttribute("user",user);
        model.addAttribute("goods",goods);
        String html=redisService.get(GoodsKey.getGoodsDetail,""+goodsId,String.class);
        if(!StringUtils.isEmpty(html))
            return html;
        long startAt=goods.getStartDate().getTime();
        long endAt=goods.getEndDate().getTime();
        long now  =System.currentTimeMillis();
        int miaoshaStatus=0;
        int remainSeconds=0;

        if(now<startAt){  //秒杀还没有开始，倒计时
            miaoshaStatus=0;
            remainSeconds= (int) ((startAt-now)/1000);
        }
        else if(now>endAt){ //秒杀已经结束了
            miaoshaStatus=2;
            remainSeconds=-1;
        }
        else{  //正在秒杀
            miaoshaStatus=1;
            remainSeconds=0;
        }
        System.out.println(goods.getStartDate());
        System.out.println(goods.getEndDate());
        model.addAttribute("miaoshaStatus",miaoshaStatus);
        model.addAttribute("remainSeconds",remainSeconds);

        SpringWebContext ctx=new SpringWebContext(req,resp,req.getServletContext(),req.getLocale(),model.asMap(),applicationContext);

        //手动渲染
        html =thymeleafViewResolver.getTemplateEngine().process("goods_detail",ctx);
        if(!StringUtils.isEmpty(html)){
            redisService.set(GoodsKey.getGoodsDetail,""+goodsId,html);
        }
        return html;
    }
    @RequestMapping(value="/detail/{goodsId}")
    @ResponseBody
    public Result<GoodsDetailVo> detail(HttpServletRequest req, HttpServletResponse resp, Model model, MiaoshaUser user,
                                        @PathVariable("goodsId")long goodsId){
        GoodsVo goods =goodsService.getGoodsVoByGoodsId(goodsId);


        long startAt=goods.getStartDate().getTime();
        long endAt=goods.getEndDate().getTime();
        long now  =System.currentTimeMillis();
        int miaoshaStatus=0;
        int remainSeconds=0;

        if(now<startAt){  //秒杀还没有开始，倒计时
            miaoshaStatus=0;
            remainSeconds= (int) ((startAt-now)/1000);
        }
        else if(now>endAt){ //秒杀已经结束了
            miaoshaStatus=2;
            remainSeconds=-1;
        }
        else{  //正在秒杀
            miaoshaStatus=1;
            remainSeconds=0;
        }
        SpringWebContext ctx=new SpringWebContext(req,resp,req.getServletContext(),req.getLocale(),model.asMap(),applicationContext);

        //手动渲染
        GoodsDetailVo vo=new GoodsDetailVo();
        vo.setGoods(goods);
        vo.setMiaoshaStatus(miaoshaStatus);
        vo.setRemainSeconds(remainSeconds);
        vo.setUser(user);
        return Result.success(vo);
    }
}
